/*
mode by xiaoxiaoyudu
github :https://github.com/xiaoxiaoyudu
qq :1907383069
blbl:两位真实好友
*/
#include "gui_time.h"
static const char *TAG = "SNTP";
extern TFT_t DEV;
extern const unsigned char xingqi[];
extern FontxFile fx16M[2];
extern FontxFile fx32M[2];
/******rtc******/
CLOCK_WP hms[3]={
	{0,24,40,40},{1,60,100,40},{2,60,160,40}
};
#ifdef CONFIG_SNTP_TIME_SYNC_METHOD_CUSTOM
void sntp_sync_time(struct timeval *tv)
{
   settimeofday(tv, NULL);
   ESP_LOGI(TAG, "Time is synchronized from custom code");
   sntp_set_sync_status(SNTP_SYNC_STATUS_COMPLETED);
}
#endif
void time_sync_notification_cb(struct timeval *tv)
{
    ESP_LOGI(TAG, "Notification of a time synchronization event");
}
void initialize_sntp(void)
{
    ESP_LOGI(TAG, "Initializing SNTP");
    sntp_setoperatingmode(SNTP_OPMODE_POLL);
    sntp_setservername(0, "ntp1.aliyun.com");
	sntp_setservername(1, "210.72.145.44");		// 国家授时中心服务器 IP 地址
    sntp_setservername(2, "1.cn.pool.ntp.org"); 
    sntp_set_time_sync_notification_cb(time_sync_notification_cb);
#ifdef CONFIG_SNTP_TIME_SYNC_METHOD_SMOOTH
    sntp_set_sync_mode(SNTP_SYNC_MODE_SMOOTH);
#endif
    sntp_init();
}
void time_init()
{

	time_t now;
	char strftime_buf[64];
    struct tm timeinfo;
	int retry = 0;

    time(&now);
    localtime_r(&now, &timeinfo);
    // Is time set? If not, tm_year will be (1970 - 1900).
    if (timeinfo.tm_year < (2016 - 1900)) 
	{
        ESP_LOGI(TAG, "Time is not set yet. Connecting to WiFi and getting time over NTP.");
        initialize_sntp();
		while (timeinfo.tm_year < (2019 - 1900)) 
		{
        	ESP_LOGD(TAG, "Waiting for system time to be set... (%d)", ++retry);
        	vTaskDelay(100 / portTICK_PERIOD_MS);
        	time(&now);
        	localtime_r(&now, &timeinfo);
    	}

    }
	setenv("TZ", "UTC+8", 1);
    tzset();
    strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo);
    ESP_LOGI(TAG, "The current date/time in Shanghai is: %s", strftime_buf);

}
void get_time(struct tm *timeinfo)
{
	time_t now;
	//获取时间
	time(&now);
	// Set timezone to China Standard Time
	setenv("TZ", "CST-8", 1);
	tzset();
	localtime_r(&now, timeinfo);
}


void change_Num(CLOCK_WP hms)
{
	if(hms.number%10==0)
	{	
		lcdShowClock(&DEV,hms.x,hms.y,hms.number,WHITE);//显示新秒数
	}
	else
	{
		if(hms.number<10)
		{
			lcdShowClock(&DEV,hms.x,hms.y,hms.number,WHITE);//显示新秒数
		}else
		{
			lcdDrawNum(&DEV,hms.x+24,hms.y,hms.number%10,WHITE);//显示新秒数
		}
		
		
	}
		
}

void clock_first()
{
	struct tm timeinfo;
		//点
	color_p(&DEV, WHITE);
	get_time(&timeinfo);
	hms[0].number=timeinfo.tm_hour;
	hms[1].number=timeinfo.tm_min;
	hms[2].number=timeinfo.tm_sec;
	for(uint8_t i=0;i<3;i++)
	{
		lcdShowClock(&DEV,hms[i].x,hms[i].y,hms[i].number,WHITE);//显示新秒数
	}
}
void show_clock()
{
	
	struct tm timeinfo;
	get_time(&timeinfo);
	//点
	static uint8_t S_show_p=0;

	if(S_show_p)
	{
	color_p(&DEV, BLACK);
	S_show_p=0;
	}
	else
	{
	color_p(&DEV, WHITE);
	S_show_p=1;
	}	
	//判断各种情况
	//数字由单数变为双数，刷新两位数
	if(timeinfo.tm_hour!=hms[0].number)
	{
		hms[0].number=timeinfo.tm_hour;
		change_Num(hms[0]);
		
	}
	if(timeinfo.tm_min!=hms[1].number)
	{
		hms[1].number=timeinfo.tm_min;
		change_Num(hms[1]);
		
	}
	if(timeinfo.tm_sec!=hms[2].number)
	{
		hms[2].number=timeinfo.tm_sec;
		change_Num(hms[2]);
		
	}	
}
void show_week()
{
	struct tm timeinfo;
	char time_data[30];
	get_time(&timeinfo);
	sprintf(time_data,"%d/%d",timeinfo.tm_mon+1,timeinfo.tm_mday);
	uint8_t week;
	if(timeinfo.tm_wday==0)
	{
		week=6;
	}
	else
	{
		week=timeinfo.tm_wday-1;
	}
	lcdDrawString(&DEV,fx16M, 15, 76, (unsigned char *)time_data, WHITE);
	LCD_ShowChinese(&DEV,50,60,7,WHITE,xingqi);//周
	LCD_ShowChinese(&DEV,66,60,week,WHITE,xingqi);//

}
void color_p(TFT_t * dev,uint16_t color)
{
	lcdDrawFillRect(&DEV, 93,55, 95, 57, color);
	lcdDrawFillRect(&DEV, 93,90, 95, 92, color);
	lcdDrawFillRect(&DEV, 153,55, 155, 57, color);
	lcdDrawFillRect(&DEV, 153,90, 155, 92, color);
}
void show_time_mini()
{
	uint8_t now_time[40];
	struct tm timeinfo;
	get_time(&timeinfo);

	sprintf((char *)now_time,"%02d:%02d",timeinfo.tm_hour,timeinfo.tm_min);
	lcd_fastDrawString(&DEV, fx32M, 10, 60, now_time, WHITE);
}